import Taro from '@tarojs/taro'
import { prefix } from '../utils'

// 我的页面 - 获取全部信息
export function fetchUser(data = {}, callback) {
    return () => {
        const userInfo = Taro.getStorageSync('userInfo') || {}
        Taro.request({
            url: `${prefix}/api/v1/dashboards`,
            data:{...data, userId: userInfo.userId},
            method: 'GET',
            header: { 'content-type': 'application/json', 'Authorization' : `Bearer ${userInfo.token}` }
        })
        .then(results => {
            const { data = {} } = results || {}
            if(callback) callback(data)
        })
    }
}

// 我的页面 - 收获幸运草
export function add(data = {}, callback) {
    return () => {
        const userInfo = Taro.getStorageSync('userInfo') || {}
        Taro.request({
            url: `${prefix}/api/v1/integralIns`,
            data: {...data, userId: userInfo.userId},
            method: 'POST',
            header: { 'content-type': 'application/json', 'Authorization' : `Bearer ${userInfo.token}` }
        })
        .then(results => {
            const { data = {} } = results || {}
            if(callback) callback(data)
        })
    }
}

// 我的页面 - 提交收货地址
export function addAddress(data = {}, callback) {
    return () => {
        const userInfo = Taro.getStorageSync('userInfo') || {}

        Taro.request({
            url: `${prefix}/api/v1/addresses`,
            data:{...userInfo, ...data},
            method: 'POST',
            header: { 'content-type': 'application/json', 'Authorization' : `Bearer ${userInfo.token}` }
        })
        .then(results => {
            const { data = {} } = results || {}
            if(callback) callback(data)
        })
    }
}